package StudentManageSystem;

import com.sun.org.apache.xerces.internal.impl.xs.SchemaNamespaceSupport;
import sun.swing.StringUIClientPropertyKey;

import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class StudentSystem {
    public static void main(String[] args) {
//        ArrayList<Student> stu = new ArrayList<>();
//        loop: while (true) {
//            System.out.println("--------------欢迎来到学生管理系统-------------");
//            System.out.println("1.添加学生");
//            System.out.println("2.删除学生");
//            System.out.println("3.修改学生");
//            System.out.println("4.查询学生");
//            System.out.println("5.退出系统");
//            System.out.println("请输入您的选择：");
//            Scanner sc = new Scanner(System.in);
//            String choose = sc.next();
//            //根据switch语句分支进行相关功能操作
//            switch (choose){
//                case "1":{
//                    AddStudent(stu);
//                    break;
//                }
//                case "2": {
//                    DeleteStudent(stu);
//                    break;
//                }
//                case "3":
//                {
//                    ModifyStudent(stu);
//                    break;
//                }
//                case "4":{
//                    InquireStudent(stu);
//                    break;
//                }
//                case "5":{
//                    System.out.println("退出成功！");
//                    break loop;
//                }
//                default:
//                    System.out.println("输入有误，请重新选择！");
//            }
//        }


        //学生管理系统升级版本

        ArrayList<User> user = new ArrayList<>();

        loop: while (true) {
            System.out.println("--------------欢迎来到学生管理系统-------------");
            System.out.println("请选择操作：1.登录 2.注册 3.忘记密码");

            Scanner sc = new Scanner(System.in);
            String choose = sc.next();
            //根据switch语句分支进行相关功能操作
            switch (choose){
                case "1":{
                    Login(user);
                    break;
                }
                case "2":{
                    register(user);
                    break;
                }
                case "3":{
                    forggetPassword(user);
                    break;
                }

                default:
                   System.out.println("输入有误，请重新选择！");

            }
        }

    }

    //增删改查操作
    public static void CURD(){
        ArrayList<Student> stu = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        loop: while (true) {
            System.out.println("--------------欢迎来到学生管理操作界面-------------");
            System.out.println("1.添加学生");
            System.out.println("2.删除学生");
            System.out.println("3.修改学生");
            System.out.println("4.查询学生");
            System.out.println("5.退出系统");
            System.out.println("请输入您的选择：");
            String choose = sc.next();
            //根据switch语句分支进行相关功能操作
            switch (choose){
                case "1":{
                    addStudent(stu);
                    break;
                }
                case "2": {
                   deleteStudent(stu);
                    break;
                }
                case "3": {
                    modifyStudent(stu);
                    break;
                }
                case "4":{
                    inquireStudent(stu);
                    break;
                }
                case "5":{
                    System.out.println("退出成功！");
                    break loop;
                }
                default:
                    System.out.println("输入有误，请重新选择！");
            }
        }
    }

    //注册用户
    public static void register(ArrayList<User> u){
        //创建一个用户并添加到集合之中
        User user = new User();

        Scanner sc = new Scanner(System.in);
        //创建用户名
        loop: while (true) {
            System.out.println("请输入用户名[用户名长度必须为3~15位,且必须为字母+数字组合]");
            String username = sc.next();

            //判断用户名是否唯一
            if(getIndexForUser(u, username) >= 0){
                System.out.println("该用户已存在，请重新输入");
                continue;
            }

            //用户名长度在3——15之间
            if(username.length() < 3 || username.length() > 15){
                System.out.println("用户名长度未达到或超出字段限制");
                continue;
            }

            int count = 0;
            for (int i = 0; i < username.length(); i++) {
                char ch = username.charAt(i);

                //合法的字母或者数字字符
                if ((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
                    if ((ch >= '0' && ch <= '9')) count++;
                } else {
                    System.out.println("用户名仅能用数字和字母表示！");
                    continue loop;
                }
            }

            if(count == username.length()){
                System.out.println("用户名不能为纯数字！");
                continue;
            }

            //用户名满足所有要求，可以正常添加了
            user.setUsername(username);
            break;
        }

        //输入密码
        while (true) {
            System.out.println("请输入密码：");
            String password1 = sc.next();
            System.out.println("请再一次输入密码：");
            String password2 = sc.next();
            if(password1.equals(password2)){
                user.setPassword(password1);
                break;
            }else{
                System.out.println("两次密码不一致，请重新输入！");
            }
        }

        //身份证号码验证
        loop2: while (true) {
            System.out.println("请输入您的身份证号码：");
            String idNumber = sc.next();

            //长度为18位，不能以0开头
            if(idNumber.length() != 18 || idNumber.charAt(0) == '0'){
                System.out.println("身份证号不合法！");
                continue;
            }

            //前十七位必须是数字
            for (int i = 0; i < idNumber.length() - 1; i++) {
                if(idNumber.charAt(i) < '0' || idNumber.charAt(i) > '9'){
                    System.out.println("身份证号不合法！");
                    continue loop2;
                }
            }

            //最后一位是数字或者大小写的X
            char last = idNumber.charAt(idNumber.length() - 1);
            if(!((last >= '0' && last <= '9') ||  (last == 'x') ||( last  == 'X'))){
                System.out.println("身份证号不合法！");
                continue;
            }

            user.setIdNumber(idNumber);
            break;
        }

        //手机号验证
        loop3:while (true) {
            System.out.println("请输入您的手机号：");
            String phoneNumber = sc.next();

            if(phoneNumber.length() != 11 || phoneNumber.charAt(0) == '0'){
                System.out.println("手机号不合法！");
                continue;
            }

            for (int i = 0; i < phoneNumber.length(); i++) {
                char ch = phoneNumber.charAt(i);
                if(ch < '0' || ch > '9'){
                    System.out.println("手机号不合法！");
                    continue loop3;
                }
            }
            user.setPhoneNumber(phoneNumber);
            break;
        }

        u.add(user);
        System.out.println("注册用户成功！");
    }



    //登录系统功能
    public static void Login(ArrayList<User> u){
        System.out.println("请输入用户名：");
        Scanner sc = new Scanner(System.in);
        String username = sc.next();
        int indexForUser = getIndexForUser(u, username);

        //用户名如果未注册，直接结束方法，并提示：用户名未注册，请先注册！
        if(indexForUser == -1){
            System.out.println("用户名未注册，请先注册!");
            return;
        }

        //判断密码是否正确，有3次机会
        int opportunity = 0;
        while (opportunity < 3) {
            System.out.println("请输入密码：");
            String password = sc.next();
            if(!(u.get(indexForUser).getPassword().equals(password))){
                opportunity++;
                if(opportunity < 3)
                    System.out.println("密码错误，请重新输入。还剩下" + (3 - opportunity) + "次机会");
            }
            else{
                break;
            }
        }

        if(opportunity >= 3) {
            System.out.println("密码输入错误超过3次机会,返回到初始界面。");
            return;
        }

        //生成验证码
        //长度为5, 4位大写或者小写字母和1位数字组成
        while (true) {
            String ret = randomCode(5);
            System.out.println("请输入验证码" + "[" + ret + "]");
            String randomCode = sc.next();
            //验证码忽略大小写
            if(ret.equalsIgnoreCase(randomCode)){
                break;
            }
        }
        System.out.println("登录成功!");
        CURD();
    }

    //用户忘记密码
    public static void forggetPassword(ArrayList<User> u){
        System.out.println("请输入用户名：");
        Scanner sc = new Scanner(System.in);
        String username = sc.next();
        int indexForUser = getIndexForUser(u, username);

        //用户名如果未注册，直接结束方法，并提示：用户名未注册，请先注册！
        if(indexForUser == -1){
            System.out.println("用户名未注册，请先注册!");
            return;
        }

        System.out.println("请输入身份证号");
        String idNumber = sc.next();
        if(!(u.get(indexForUser).getIdNumber().equalsIgnoreCase(idNumber))){
            System.out.println("账号信息不匹配，修改失败");
            return;
        }

        System.out.println("请输入手机号");
        String phoneNumber = sc.next();
        if(!(u.get(indexForUser).getPhoneNumber().equals(phoneNumber))){
            System.out.println("账号信息不匹配，修改失败");
            return;
        }

        System.out.println("请输入密码，进行修改:");
        String newPasswd = sc.next();
        u.get(indexForUser).setPassword(newPasswd);
        System.out.println("密码修改成功！");
    }

    //添加学生
    public static void addStudent(ArrayList<Student> stu){
        Student student = null;
        Scanner sc = null;
        String id = null;
        while (true) {
            student = new Student();
            System.out.println("请输入学生的id");
            sc = new Scanner(System.in);
            id = sc.next();

            if(contains(stu,id)){
                System.out.println("该学生id已存在，请重新输入！");
            }
            else{
                break;
            }
        }
        student.setId(id);

        System.out.println("请输入学生的姓名");
        String name = sc.next();
        student.setName(name);

        System.out.println("请输入学生的年龄");
        int age = sc.nextInt();
        student.setAge(age);


        System.out.println("请输入学生的家庭住址");
        String address = sc.next();
        student.setAddress(address);

        stu.add(student);
        System.out.println("学生信息添加成功！");
    }

    //删除学生
    public static void deleteStudent(ArrayList<Student> stu){

        System.out.println("请输入要删除的学生id");
        Scanner sc = new Scanner(System.in);
        String id = sc.next();
        int index = getIndex(stu, id);
        if(index == -1){
            System.out.println("id不存在，返回到初始菜单");
            return;
        }
        stu.remove(index);
        System.out.println("id为" + id + "学生删除成功!");
    }
    //查询学生
    public static void inquireStudent(ArrayList<Student> stu){

        if(stu.size() == 0){
            System.out.println("当前无学生信息，请添加后查询！");
            return;
        }

        System.out.println("id\t姓名\t年龄\t家庭住址");
        for (int i = 0; i < stu.size(); i++) {

            Student student = stu.get(i);
            System.out.println(student.getId() + "\t" + student.getName() +
                    "\t" + student.getAge() +  "\t"+ student.getAddress());
        }
    }

    //修改学生
    public static void modifyStudent(ArrayList<Student> stu){
        System.out.println("请您输入要修改的学生id");
        Scanner sc = new Scanner(System.in);
        String id = sc.next();
        int index = getIndex(stu, id);
        if(index == -1){
            System.out.println("id不存在，返回到初始菜单");
            return;
        }


        System.out.println("请您输入要修改的学生姓名");
        String newName = sc.next();
        stu.get(index).setName(newName);

        System.out.println("请您输入要修改的学生年龄");
        int newAge = sc.nextInt();
        stu.get(index).setAge(newAge);

        System.out.println("请您输入要修改的学生家庭住址");
        String newAddress = sc.next();
        stu.get(index).setAddress(newAddress);

        System.out.println("学生信息修改成功！");
    }

    //id是否在学生集合中已经存在
    public static boolean contains(ArrayList<Student> stu, String id){

//        for (int i = 0; i < stu.size(); i++) {
//            Student student = stu.get(i);
//            if(student.getId().equals(id)){
//                return true;
//            }
//        }
//        return false;

        return getIndex(stu, id) >= 0;
    }
    //获取id在学生集合中的索引位置
    public static int getIndex(ArrayList<Student> stu, String id){
        for (int i = 0; i < stu.size(); i++) {
            Student student = stu.get(i);
            if(student.getId().equals(id)){
                return i;
            }
        }
        return -1;
    }

    //获取Username在用户集合中的索引位置
    public static int getIndexForUser(ArrayList<User> u, String username){
        for (int i = 0; i < u.size(); i++) {
            User user = u.get(i);
            if(user.getUsername().equals(username)){
                return i;
            }
        }
        return -1;
    }

    //生成验证码,number表示验证码的位数
    public static String randomCode(int number){
        //定义字符数组，用于存储的验证码的组成
        char[] arr = new char[62];

        int index = 0;
        for (char ch = '0'; ch <= '9'; ch++) {
            arr[index] = ch;
            index++;
        }

        for (char ch = 'a'; ch <= 'z'; ch++) {
            arr[index] = ch;
            index++;
        }

        for (char ch = 'A'; ch <= 'Z'; ch++) {
            arr[index] = ch;
            index++;
        }

        //记录返回的验证码
        String ret = "";
        Random random = new Random();

        boolean flag = false;//表示是否已经生成了一位数字
        int n = 0;
        int tmp;//表示每次生成的随机索引值

        //随机生成number位验证码
        while(true){
            //已经生成了一位数字，后面就直接在a~z,A`Z中随机
            if(flag){
                tmp = random.nextInt(arr.length - 10) + 10;
                ret += arr[tmp];
                n++;
            }
            else{
                tmp = random.nextInt(arr.length);
                ret += arr[tmp];
                if(tmp >= 0 && tmp <= 9) flag = true;
                n++;
            }

            if(n == number && flag) break;

            if(n > number) {
                ret = "";
                flag = false;
                n = 0;
            };
        }
        return ret;
    }


}
